package org.testis.data.parser;

import org.testis.data.*;
import org.testis.logging.AtofLogLevel;
import org.testis.webautomationmodule.data.WebAutomationModuleDataObject;
import org.testis.logging.LogSenderService;
import org.testis.testdriver.AtofEnumaration.AtofWebAutomationModuleAction;
import org.orm.PersistentException;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;


/**
 * Defines a class to parse a TestStepDataObject into WebAutomationModuleDataObject
 * Will use dutMap tree generated using ATOF Database Layer Objects 
 * @author nurhat
 */
public class WebAutomationModuleDop implements TestStepDop
{

    private WebAutomationModuleDataObject m_data;
    private DynamicMapDesignTreeNode m_root;


    /**
     * Makes conversion from TestStepDataObject to WebAutomationModuleDataObject
     * @param data TestStepDataObject to be converted into WebAutomationModuleDataObject
     * @return Generates a WebAutomationModuleDataObject as output
     */
    public TestStepDataObject convert(TestStepDataObject data)
    {

        if (data != null) {
            m_data = new WebAutomationModuleDataObject(data);
        }
        else {
            return null;
        }
        try {
            String deviceId = null;
            if (m_data.getModuleDataToApply().containsKey(
                AtofModuleParameterNames.WEB_AUTO_DEVICE_ID)) {
                deviceId = m_data.getModuleDataToApply().get(
                    AtofModuleParameterNames.WEB_AUTO_DEVICE_ID);
            }
            try {
                m_root = DynamicMapTreeProviderFactory.getInstance().
                    getDynamicMapTree(deviceId);
            } catch (PersistentException ex) {
                LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "Could not get Device map from database with id " + deviceId,
                    m_data.getMessageId());
            }
            if (m_root == null) {
                LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "Device map not found for the device with id " + deviceId,
                    data.getMessageId());
                return null;
            }
            WAMLoginDopDecorator loginDeco =
                new WAMLoginDopDecorator(m_data,
                                         m_root);
            WAMPageContentVerificationDopDecorator pageContentVerificationDeco =
                null;
            WAMHandleDutIpDopDecorator handleDutIpDopDeco = null;
            WAMLanguageSelectionDopDecorator languageSelectionDopDeco =
                null;
            WAMSetAndGetDopDecorator setAndGetDopDecorator =
                new WAMSetAndGetDopDecorator(m_data,
                                             m_root);
            if (data.getActionType().equals(
                AtofWebAutomationModuleAction.HandleDutIp.name())) {
                handleDutIpDopDeco =
                    new WAMHandleDutIpDopDecorator(m_data,
                                                   m_root);
                loginDeco.setNextDecoratorObject(handleDutIpDopDeco);
                handleDutIpDopDeco.setNextDecoratorObject(setAndGetDopDecorator);
            }
            else if (data.getActionType().equals(
                AtofWebAutomationModuleAction.PageContentVerification.name())) {
                pageContentVerificationDeco =
                    new WAMPageContentVerificationDopDecorator(m_data,
                                                               m_root);
                loginDeco.setNextDecoratorObject(pageContentVerificationDeco);
                pageContentVerificationDeco.setNextDecoratorObject(
                    setAndGetDopDecorator);
            }
            else if (data.getActionType().equals(
                AtofWebAutomationModuleAction.LanguageSelection.name())) {
                languageSelectionDopDeco =
                    new WAMLanguageSelectionDopDecorator(m_data,
                                                         m_root);
                loginDeco.setNextDecoratorObject(languageSelectionDopDeco);
                languageSelectionDopDeco.setNextDecoratorObject(
                    setAndGetDopDecorator);
            }
            else {
                loginDeco.setNextDecoratorObject(setAndGetDopDecorator);
            }
            boolean isParsed = loginDeco.operate();
            if (isParsed) {
                return loginDeco.getData();
            }
            else {
                LogSenderService.putMessageOnQueue(
                    "WebAutomationDataObjectParser",
                    AtofLogLevel.ERROR,
                    "Could not append login page data. Login page could not found ",
                    data.getMessageId());
            }

        } catch (Exception ex) {
            LogSenderService.putMessageOnQueue(
                "WebAutomationDataObjectParser",
                AtofLogLevel.ERROR,
                "Could not parse web automation data object." + ex.toString(),
                data.getMessageId());
            ex.printStackTrace();
        }
        return null;
    }
}
